const express = require("express");
const api = express.Router();
const auth = require("../middleware/auth");

api.post("/login", (req, res) => {
    const { username, password } = req.body;
    if (username === "admin" && password === "123456") {
        req.session.user = {
            name: username,
        };
        res.send({
            code: 200,
            message: "登录成功",
            data: {
                name: username,
            },
        });
    } else {
        res.send({
            code: 401,
            message: "用户名或密码错误",
            data: null,
        });
    }
});

api.get("/", auth, function (req, res) {
    res.send({
        code: 200,
        message: "获取成功",
        data: {
            name: req.user.name,
        },
    });
});

module.exports = api;
